#include "game.h"
#include "display.c"
#include "btns.c"
#include "uart.c"
#include "timer.c"
#include "game.c"

void process_start()
{
    game_setting.score = 0;
    game_setting.speed = 0;
    game_zone_data_init();
    display_game_start_screen();
    btn_util_press(BTN_CODE_START);
    game_setting.state = STATE_SPEED_SELECTION;
}

void process_speed_selection()
{
    unsigned char btn_code;
    display_speed_screen();
    do
    {
        btn_code = btn_scan();

        if (btn_code == BTN_CODE_LEFT)
        {
            if (game_setting.speed == 0)
            {
                game_setting.speed = 9;
            }
            else
            {
                game_setting.speed--;
            }
            display_speed_in_selection();
        }
        else if (btn_code == BTN_CODE_RIGHT)
        {
            if (game_setting.speed == 9)
            {
                game_setting.speed = 0;
            }
            else
            {
                game_setting.speed++;
            }
            display_speed_in_selection();
        }

    } while (btn_code != BTN_CODE_START);
    game_setting.state = STATE_PLAYING;
}

void process_playing()
{
    unsigned char btn_code;
    display_game_screen();
    game_zone_new_shape();
    display_next_shape();
    timer0_isr_on();
    while (game_setting.state == STATE_PLAYING)
    {
        btn_code = btn_scan();
        switch (btn_code)
        {
        case BTN_CODE_UP:
            break;
        case BTN_CODE_DOWN:
            break;
        case BTN_CODE_LEFT:
            break;
        case BTN_CODE_RIGHT:
            break;
        case BTN_CODE_PAUSE:
            timer0_isr_off();
            display_pause();
            btn_util_press(BTN_CODE_PAUSE);
            display_pause_hide();
            timer0_isr_on();
            break;
        default:
            break;
        }
    }
}

void process_game_over()
{
    display_game_over_screen();
    btn_util_press(BTN_CODE_START);
    game_setting.state = STATE_START;
}

void timer0_ISR() interrupt 1
{
    if (game_setting.state != STATE_PLAYING)
    {
        return;
    }

    if (game_frame_count < GAME_FRAME)
    {
        game_frame_count += (game_setting.speed + 1);
    }
    else
    {
        game_frame_count = 0;
        game_zone_remove_shape();
        if (game_zone_check_shape(game_shape.x, game_shape.y + 1, game_shape.shape))
        {
            game_shape.y++;
        }
        else
        {
            // 方块固定
            game_zone_add_shape();
            game_zone_next_shape();
            display_next_shape();
            // 检查新生成的方块是否可以放到游戏区
            // 如果新生成的方块无法放置到游戏区，游戏结束
            if (!game_zone_check_shape(game_shape.x, game_shape.y, game_shape.shape))
            {
                // 放不进去，游戏结束
                game_setting.state = STATE_GAME_OVER;
            }
        }
        game_zone_add_shape();
        display_refresh_game_zone();
    }
}

void main()
{
    uart_init();
    timer0_init();
    display_init();
    while (1)
    {
        switch (game_setting.state)
        {
        case STATE_START:
            process_start();
            break;
        case STATE_SPEED_SELECTION:
            process_speed_selection();
            break;
        case STATE_PLAYING:
            process_playing();
            break;
        case STATE_GAME_OVER:
            process_game_over();
            break;
        default:
            break;
        }
    }
}